﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EMR
{
    public partial class Main : Form
    {
        private string _Message;
        public Main()
        {
            InitializeComponent();
            loadPatient();
        }

        private void btn_AddPatient_Click(object sender, EventArgs e)
        {
            frm_AddPatient addPatientForm = new frm_AddPatient();
            addPatientForm.ShowDialog();
            addPatientForm.StartPosition = FormStartPosition.CenterScreen;
            //添加成功后用于刷新界面
            if (addPatientForm.DialogResult == DialogResult.OK)
            {
                this.loadPatient();
            }

        }

        public void loadPatient()
        {
           // SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
            listView1.View = View.LargeIcon;
            listView1.LargeImageList = imageList1;
            listView1.LargeImageList = imageList1;//绑定图标列表

            //string sql = "SELECT  *  FROM tb_Patient AS P ";
            //var dr = (SqlDataReader)SqlHelper.ExecuteQuery(sql);
            var dr = PatientBll.LoadAll();
            listView1.Clear();
            while (dr.Read())
            {
                string AdmissionNo = dr["AdmissionNo"].ToString();
                string BedNo = dr["BedNo"].ToString();
                string Gender = dr["Gender"].ToString();
                string Name = dr["Name"].ToString();
                ListViewItem lvi = new ListViewItem();                                                  //首先创建一个ListView项item
                lvi.Text = "床号:" + BedNo + "\n" + "姓名:" + Name + "\n" + "住院号:" + AdmissionNo + "\n"; //该项的文本
                lvi.ImageIndex = Gender == "男" ? 0 : 1;
                //    lvi.SubItems.AddRange(new string[] { AdmissionNo, BedNo, Gender, Name }); //添加该项的子项，，也就是第2，3，4列的值
                listView1.Items.Add(lvi);                               //最后把创建的项加到ListView控件的项中
            }
           // sqlDataAdapter.Dispose();
        }

        public void button1_Click(object sender, EventArgs e)
        {
            loadPatient();
        }

        private void btn_EditPatient_Click(object sender, EventArgs e)
        {

            try  //防止未选择病人直接点击编辑病人。
            {
                string message = listView1.SelectedItems[0].SubItems[0].Text;
                frm_EditPatient editPatientForm = new frm_EditPatient(message);
                editPatientForm.ShowDialog();
                editPatientForm.StartPosition = FormStartPosition.CenterScreen;
                //修改成功后用于刷新界面
                if (editPatientForm.DialogResult == DialogResult.OK)
                {
                    this.loadPatient();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请选择一个病人！");
            }
           
        }

        /// <summary>
        /// 删除病人
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_DeletePatient_Click(object sender, EventArgs e)
        {
            try
            {
                _Message = listView1.SelectedItems[0].SubItems[0].Text;
                string[] B = new string[3];
                int a, b, c;
                for (int i = 0; i < 3; i++)
                {
                    a = _Message.IndexOf(":");
                    b = _Message.IndexOf("\n");
                    c = _Message.Length;
                    if (b < a - 1)
                    {
                        break;
                    }
                    B[i] = _Message.Substring(a + 1, b - a - 1);//获取床号,姓名，住院号；
                    _Message = _Message.Substring(b + 1, c - b - 1);
                }
                string _BedNo = B[0];
                string _Name = B[1];
                string _AdmissionNo = B[2];
                //string sql = "DELETE  tb_Patient  WHERE BedNo=@BedNo AND Name=@Name AND AdmissionNo =@AdmissionNo;";
                //SqlParameter[] pms = new SqlParameter[]
                //{
                //new SqlParameter("@BedNo", _BedNo),
                //new SqlParameter("@Name", _Name),
                //new SqlParameter("@AdmissionNo", _AdmissionNo)
                // };
                //SqlHelper.ExecuteNonQuery(sql,false, pms);
                PatientBll.DeletePatient(_BedNo, _Name, _AdmissionNo);
                loadPatient();  //重新载入信息
            }
            catch (Exception)
            {
                MessageBox.Show("请选择一个病人！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            var dr = PatientBll.SelectOne(this.txb_Search.Text);
            listView1.Clear();
            while (dr.Read())
            {
                string AdmissionNo = dr["AdmissionNo"].ToString();
                string BedNo = dr["BedNo"].ToString();
                string Gender = dr["Gender"].ToString();
                string Name = dr["Name"].ToString();
                ListViewItem lvi = new ListViewItem();                                                  //首先创建一个ListView项item
                lvi.Text = "床号:" + BedNo + "\n" + "姓名:" + Name + "\n" + "住院号:" + AdmissionNo + "\n"; //该项的文本
                lvi.ImageIndex = Gender == "男" ? 0 : 1;
                //    lvi.SubItems.AddRange(new string[] { AdmissionNo, BedNo, Gender, Name }); //添加该项的子项，，也就是第2，3，4列的值
                listView1.Items.Add(lvi);                               //最后把创建的项加到ListView控件的项中
            }


        }

        private void btn_Template_Click(object sender, EventArgs e)
        {
            // TemplateManagement templateManagement = new TemplateManagement();
            frm_ManageCaseModel templateManagement = new frm_ManageCaseModel();
            templateManagement.ShowDialog();
            templateManagement.StartPosition = FormStartPosition.CenterScreen;

        }

        private void btn_temDesign_Click(object sender, EventArgs e)
        {
            TemplateDesign templateDesign = new TemplateDesign();
            templateDesign.ShowDialog();
            templateDesign.WindowState = FormWindowState.Maximized;
            //   templateDesign.StartPosition = FormStartPosition.CenterScreen;

        }
    }
}
